#!/bin/awk -f 
BEGIN {
        inRequest=0;
        requestNum=0;
        curRootNum=0;
        firsttime="";
        lasttime="";
}
{
        if (requestNum == 1) {
                firsttime       = lasttime;
        }
        if (inRequest > 0) {
                if ($1 != "") {
                        if (index($0, "dump failed") > 0) {
                                next;
                        }
                        lineNum = substr($NF, index($NF, ":") + 1);
                        curRequest      = curRequest$2".";
                        curLineNums     = curLineNums""lineNum".";
                        curLines        = curLines$0"\n";
                        curRootNum++;
                        if (curRootNum == 1) {
                                lastStatRequests[$2]++;
                        } else if (curRootNum == 2) {
                                l2StatRequests[curRequest]++;
                        }
                } else {
                        if (!curLines) {
                                next;
                        }
                        inRequest       = 0;
                        requestKey      = curRequest" "curLineNums;
                        requests[requestKey]++;
                        if (!requestLines[requestKey]) {
                                requestLines[requestKey]        = curLines;
                        }
                        requestNum++;
                        curRequest      = "";
                        curLines        = "";
                        curLineNums     = "";
                        curRootNum      = 0;
                }
        } else {
                if ($1 == "script_filename") {
                        inRequest       = 1;
                        curFilename     = $3;
                } else if (index($2, ":") > 0) {
                        lasttime        = $1" "$2;
                }
        }
}
END {
        print "#Total request num: "requestNum;
        print "# " firsttime " ~ " lasttime;

        limitNum        = requestNum / 200;
        len     = 0;
        for (request in requests) {
                curRequestNum   = requests[request];
                if (curRequestNum < limitNum) {
                        continue;
                }
                len++;
                requestNumArr[len]      = curRequestNum;
                numKeyNum[curRequestNum]++;
                numKey  = numKeyNum[curRequestNum];
                validRequests[curRequestNum,numKey]     = request;
        }

        len     = asort(requestNumArr, sortRequestNumArr);
        n       = 0;
        for (i = len; i >= 1; i--) {
                num     = sortRequestNumArr[i];
                for (j = 1; j <= numKeyNum[num]; j++) {
                        n++;
                        k       = num SUBSEP j;
                        validRequestStr = substr(validRequests[k], 0, index(validRequests[k], " "));
                        printf("# %-2s    %-7d    %6.3f%%    %s\n", n, num, num/requestNum*100, substr(validRequestStr , 0, 80));
                }
        }

        print "";
        limitLastNum    = requestNum / 10;
        print "#last call stat";
        lastLen = 0;
        for (request in lastStatRequests) {
                curRequestNum   = lastStatRequests[request];
                if (curRequestNum < limitLastNum) {
                        continue;
                }
                lastLen++;
                lastRequestNumArr[lastLen]      = curRequestNum;
                lastNumKeyNum[curRequestNum]++;
                lastNumKey      = lastNumKeyNum[curRequestNum];
                validLastStatRequests[curRequestNum,lastNumKey] = request;
        }

        lastLen = asort(lastRequestNumArr, lastSortRequestNumArr);
        n       = 0;
        for (i = lastLen; i >= 1; i--) {
                num     = lastSortRequestNumArr[i];
                for (j = 1; j <= lastNumKeyNum[num]; j++) {
                        n++;
                        k       = num SUBSEP j;
                        printf("# %-2s    %-7d    %6.3f%%    %s\n", n, num, num/requestNum*100, validLastStatRequests[k]);
                }
        }

        print "";

        n       = 0;
        for (i = len; i >= 1; i--) {
                num     = sortRequestNumArr[i];
                for (j = 1; j <= numKeyNum[num]; j++) {
                        n++;
                        k       = num SUBSEP j;
                        request = validRequests[k];
                        print "----------------"n"----------------";
                        print "calls: "num;
                        print "percent: "num/requestNum*100"%";
                        print substr(validRequests[k], 0, index(validRequests[k], " "));
                        print requestLines[request];
                }
        }
#       for (request in validRequests) {
#               i++;
#               trueRequest     = substr(request, 0, 50); 
#               print i ". " trueRequest" "validRequests[request];
#       }
#       
#       print "";
#       print "";
#
#       i       = 0;
#       for (request in validRequests) {
#               i++;
#               print "-----------" i "----------";
#               print request;
#               trueRequest     = substr(request, 0, index(request, " "));
#               print requestLines[request];
#               print "";
#       }
}

